package com.taihua_tech.basic.util;

import java.math.BigDecimal;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtil  {
	public static final String BLANK = "";
	public static final String NULL = "null";

	public static boolean isNullOrEmpty(String value) {
		return (value == null || value.trim().equals("") || "null".equals(value
				.trim()));
	}

	public static String upperFirst(String value) {
		return value.substring(0, 1).toUpperCase()
				+ value.substring(1, value.length());
	}

	public static String lowerFirst(String value) {
		return value.substring(0, 1).toLowerCase()
				+ value.substring(1, value.length());
	}

	public static String lastCharacter(String value) {
		String result = null;
		result = value.substring(value.length() - 1, value.length());
		return result;
	}

	public static String firstCharacter(String value) {
		String result = null;
		result = value.substring(0, 1);
		return result;
	}

	public static String append(String[] values, String spliter) {
		String result = "";
		for (int i = 0; i < values.length; i++) {
			result = result + values[i];
			if (i < values.length - 1) {
				result = result + spliter;
			}
		}
		return result;
	}

	private static final int MAXASCII = 127;

	public static String getNotNullString(String str) {
		return str == null ? "" : str;
	}

	public static String getNotNullAndTrimString(String str) {
		return str == null ? "" : str.trim();
	}

	public static String getNullWhenEmpty(String str) {
		return (str != null) && (str.length() == 0) ? null : str;
	}

	public static String getNullWhenWhiteSpace(String str) {
		return (str != null) && (str.trim().length() == 0) ? null : str;
	}

	public static String getNullWhenWhiteSpaceOrTrim(String str) {
		return (str != null) && (str.trim().length() == 0) ? null : str.trim();
	}

	public static String getNullWhenEmptyOrUnspecified(String str,
			String unspecifiedStr) {
		if ((str != null)
				&& ((str.length() == 0) || (str.equals(unspecifiedStr)))) {
			return null;
		}
		return str;
	}
	public static String string2Number(String s){
		String result = "";
		char[] chars = s.toCharArray();
		for (int i = 0;i < chars.length;i++){
			result = result + String.valueOf((int)chars[i])+ " ";
		}
		return result.trim();
	}
	public static String number2String(String s){
		String result = "";
		String[] chars = s.split(" ");
		for (int i=0;i < chars.length;i++){
			char temp = (char)Integer.parseInt(chars[i]);
			result = result + String.valueOf(temp);
		}
		return result;
	}
	public static String getNullOrTrim(String str) {
		return str == null ? null : str.trim();
	}

	public static boolean isNotNullNotEmpty(String str) {
		return (str != null) && (str.trim().length() > 0);
	}

	public static String initialUpperCase(String str) {
		if ((str == null) || (str.length() == 0)) {
			return str;
		}
		char ch = str.charAt(0);
		if (!Character.isUpperCase(ch)) {
			str = Character.toUpperCase(ch) + str.substring(1);
		}
		return str;
	}

	public static String initialLowerCase(String str) {
		if ((str == null) || (str.length() == 0)) {
			return str;
		}
		char ch = str.charAt(0);
		if (!Character.isLowerCase(ch)) {
			str = Character.toLowerCase(ch) + str.substring(1);
		}
		return str;
	}

	public static String trimLeadingTabsAndSpaces(String line) {
		int size = line.length();
		int start = size;
		for (int i = 0; i < size; i++) {
			char c = line.charAt(i);
			if (!isIndentChar(c)) {
				start = i;
				break;
			}
		}
		if (start == 0)
			return line;
		if (start == size) {
			return "";
		}
		return line.substring(start);
	}

	public static boolean isIndentChar(char ch) {
		return (Character.isWhitespace(ch)) && (!isLineDelimiterChar(ch));
	}

	public static boolean containsAlphaNumeric(String str) {
		int strLen;
		if ((str == null) || ((strLen = str.length()) == 0))
			return false;
		for (int i = 0; i < strLen; i++) {
			char c = str.charAt(i);
			if (!Character.isWhitespace(c)) {
				return true;
			}
		}
		return false;
	}

	public static boolean isInteger(String s) {
		try {
			Integer.parseInt(s.trim());
		} catch (Throwable localThrowable) {
			return false;
		}
		return true;
	}

	public static boolean isLineDelimiterChar(char ch) {
		return (ch == '\n') || (ch == '\r');
	}

	public static boolean isEqual(String str1, String str2) {
		if (str1 == null) {
			return str2 == null;
		}
		return str1.equals(str2);
	}

	public static boolean isLong(String text) {
		try {
			Long.parseLong(text.trim());
		} catch (Throwable localThrowable) {
			return false;
		}
		return true;
	}

	public static String formatLong(long ycoordinateValue) {
		String longStr = Long.toString(ycoordinateValue);
		int size = longStr.length();
		if (size > 3) {
			int count = size / 3;
			if (size % 3 == 0) {
				count--;
			}
			StringBuffer formatStr = new StringBuffer();
			int beginIndex = 0;
			for (int i = count; i > 0; i--) {
				int endIndex = size - i * 3;
				formatStr.append(longStr.substring(beginIndex, endIndex))
						.append(",");
				beginIndex = endIndex;
			}
			formatStr.append(longStr.substring(beginIndex));
			return formatStr.toString();
		}
		return longStr;
	}

	public static String bytesToMega(long v) {
		double value = v;
		double kiloBytes = value / 1024.0D;
		if (kiloBytes == 0.0D) {
			return value + " bytes";
		}
		double megaBytes = kiloBytes / 1024.0D;
		if (megaBytes == 0.0D) {
			BigDecimal bigKiloBytes = new BigDecimal(value).divide(
					new BigDecimal(1024.0D), 2, 6);
			return bigKiloBytes.toString() + " KB";
		}
		BigDecimal bigMegaBytes = new BigDecimal(value).divide(new BigDecimal(
				1048576.0D), 2, 6);
		return bigMegaBytes.toString() + " MB";
	}

	public static int toASCCharLength(String value) {
		if (value == null)
			return 0;
		char[] chars = value.toCharArray();
		int len = 0;
		for (int i = 0; i < chars.length; i++) {
			if (chars[i] > '')
				len += 2;
			else {
				len++;
			}
		}
		return len;
	}

	public static boolean isNumeric(String str) {
		Pattern pattern = Pattern.compile("(-?[^\\D]+)|(-?[^\\D]+\\.?[^\\D]+)");
		Matcher isNum = pattern.matcher(str);

		return isNum.matches();
	}

	public static boolean isEL(String str) {
		Pattern pattern = Pattern.compile("^#\\u007B[\\s|\\S]*}$");
		Matcher isEL = pattern.matcher(str);

		return isEL.matches();
	}

	public static boolean isEndsWith(String str, List<String> suffixs) {
		if (suffixs == null) {
			return false;
		}

		for (String suffix : suffixs) {
			if (str.endsWith(suffix)) {
				return true;
			}
		}

		return false;
	}

	public static String toHiddenChar(String value) {
		if (value == null) {
			return null;
		}

		char[] hidden = new char[value.length()];
		int i = 0;
		for (int size = value.length(); i < size; i++) {
			hidden[i] = '*';
		}

		return String.valueOf(hidden);
	}

}